<template>
  <el-dialog :title="title" v-model="visible" :destroy-on-close="true">
    <el-form :model="data" :rules="rules" ref="formRef" label-width="80px">
      <el-form-item label="" prop="type">
        <el-select v-model="data.type" placeholder="Select">
          <el-option
            v-for="item in typeMap"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="" prop="mode">
        <el-select v-model="data.rules.mode" placeholder="Select">
          <el-option
            v-for="item in modeMap"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item> -->
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="create">创 建</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import server from '@/utils/server'

import { type FormInstance, ElMessage } from 'element-plus'
import { cloneDeep } from 'lodash-es'
import type { PaperPart } from '@/types/paper'
const typeMap = [{ label: '选择题', value: 1 }]
const visible = ref(false)
const formRef = ref<FormInstance>()
const paperId = ref(0)
const data = ref<PaperPart>({
  type: 1,
  ids: [],
  questions: []
})
const title = '创建板块'
const rules = {
  name: [{ required: 'true', message: '名称不能为空', trigger: ['change'] }]
}

// 开启弹窗
const open = async (id: number) => {
  paperId.value = id
  visible.value = true
  data.value = {
    type: 1,
    ids: [],
    questions: []
  }
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

// 通知重新装在数据
const emits = defineEmits(['reload'])
const create = async () => {
  formRef.value?.validate(async (valid: boolean) => {
    if (!valid) {
      return
    }
    await server.post('/writer/exam/paper/part/create', {
      paperId: paperId.value,
      type: data.value.type
    })
    emits('reload') // TODO 没刷新页面
    ElMessage.success('创建成功')
    close()
  })
}

defineExpose({ open, close })
</script>

<style scoped></style>
